package com.campous.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.campous.constant.MessageConstant;
import com.campous.constant.StatusConstant;
import com.campous.dto.SetmealDTO;
import com.campous.dto.SetmealPageQueryDTO;
import com.campous.entity.Dish;
import com.campous.entity.Setmeal;
import com.campous.entity.SetmealDish;
import com.campous.exception.DeletionNotAllowedException;
import com.campous.exception.SetmealEnableFailedException;
import com.campous.mapper.DishMapper;
import com.campous.mapper.SetmealDishMapper;
import com.campous.mapper.SetmealMapper;
import com.campous.result.PageResult;
import com.campous.service.SetmealService;
import com.campous.vo.DishItemVO;
import com.campous.vo.SetmealVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
@Slf4j
public class SetmealServiceImpl implements SetmealService {
    @Autowired
    private SetmealMapper setmealMapper;
    @Autowired
    private SetmealDishMapper setmealDishMapper;
    @Autowired
    private DishMapper dishMapper;

    /**
     * 新增套餐数据
     *
     * @param setmealDTO
     */
    @Override
    public void saveWithDish(SetmealDTO setmealDTO) {
        //向套餐表插入新增的套餐数据
        Setmeal setmeal = new Setmeal();
        BeanUtils.copyProperties(setmealDTO, setmeal);
        setmealMapper.insert(setmeal);
        //获取插入的菜品数据id
        Long setmealId = setmeal.getId();
        List<SetmealDish> setmealDishes = setmealDTO.getSetmealDishes();
        //给关联的菜品套餐表增加套餐id
        setmealDishes.forEach(setmealDish -> setmealDish.setSetmealId(setmealId));
        //批量向菜品套餐表插入关联表数据
        setmealDishMapper.insertBatch(setmealDishes);
    }

    /**
     * 套餐数据分页查询
     *
     * @param setmealPageQueryDTO
     * @return
     */
    @Override
    public PageResult pageQuery(SetmealPageQueryDTO setmealPageQueryDTO) {
        //设置分页数据
        PageHelper.startPage(setmealPageQueryDTO.getPage(), setmealPageQueryDTO.getPageSize());
        Page<SetmealVO> page = setmealMapper.pageQuery(setmealPageQueryDTO);
        //设置返回分页查询的数据
        return new PageResult(page.getTotal(), page.getResult());
    }

    /**
     * 批量删除套餐数据
     *
     * @param ids
     */
    @Override
    public void deleteBatch(List<Long> ids) {
        ids.forEach(id -> {
            //1.获取套餐中的菜品数据
            Setmeal setmeal = setmealMapper.getById(id);
            //如果套餐为起售状态不能进行删除
            if (StatusConstant.ENABLE == setmeal.getStatus()) {
                //起售中的套餐不能够进行删除
                throw new DeletionNotAllowedException(MessageConstant.SETMEAL_ON_SALE);
            }
        });
        ids.forEach(setmealId->{
            //删除套餐中的数据
            setmealMapper.deleteById(setmealId);
            //删除套餐菜品关联表中的数据
            setmealDishMapper.deleteById(setmealId);
        });
    }

    /**
     * 根据id查询套餐数据
     * @param id
     * @return
     */
    @Override
    public SetmealVO getByIdWithDish(Long id) {
        SetmealVO setmealVO = new SetmealVO();
        //获取套餐数据
        Setmeal setmeal = setmealMapper.getById(id);
        BeanUtils.copyProperties(setmeal,setmealVO);
        //获取套餐关联的菜品数据
        List<SetmealDish> setmealDishes = setmealDishMapper.getBySetmealId(id);
        setmealVO.setSetmealDishes(setmealDishes);
        return setmealVO;
    }

    /**
     * 修改套餐信息
     * @param setmealDTO
     */
    @Override
    public void update(SetmealDTO setmealDTO) {
        Setmeal setmeal = new Setmeal();
        BeanUtils.copyProperties(setmealDTO,setmeal);
        //先修改套餐表的信息
        setmealMapper.update(setmeal);
        //为了方便数据的更新，我们先删除关联的菜品信息，再重新添加菜品信息
        //获取套餐id
        Long setmealId = setmealDTO.getId();
        //删除关联的套餐信息
        setmealDishMapper.deleteById(setmealId);
        List<SetmealDish> setmealDishes = setmealDTO.getSetmealDishes();
        //重新注入套餐id
        setmealDishes.forEach(setmealDish -> {
            setmealDish.setSetmealId(setmealId);
        });
        //重新插入关联的菜品信息
        setmealDishMapper.insertBatch(setmealDishes);
    }

    /**
     * 控制菜品的停售和起售
     * @param status
     * @param id
     */
    @Override
    public void startOrStop(Integer status, Long id) {
        //起售套餐时，判断套餐内是否有停售菜品，有停售菜品提示"套餐内包含未启售菜品，无法启售"
        if(status == StatusConstant.ENABLE){
            //select a.* from dish a left join setmeal_dish b on a.id = b.dish_id where b.setmeal_id = ?
            List<Dish> dishList = dishMapper.getBySetmealId(id);
            if(dishList != null && dishList.size() > 0){
                dishList.forEach(dish -> {
                    if(StatusConstant.DISABLE == dish.getStatus()){
                        throw new SetmealEnableFailedException(MessageConstant.SETMEAL_ENABLE_FAILED);
                    }
                });
            }
        }

        Setmeal setmeal = Setmeal.builder()
                .id(id)
                .status(status)
                .build();
        setmealMapper.update(setmeal);
    }
    /**
     * 条件查询
     * @param setmeal
     * @return
     */
    public List<Setmeal> list(Setmeal setmeal) {
        List<Setmeal> list = setmealMapper.list(setmeal);
        return list;
    }

    /**
     * 根据id查询菜品选项
     * @param id
     * @return
     */
    public List<DishItemVO> getDishItemById(Long id) {
        return setmealMapper.getDishItemBySetmealId(id);
    }

}
